<{capture name="header"}>
<{css app="ome" src="ome.css"}>
<{css app="ome" src="style.css"}>
<{script src="coms/autocompleter.js" app="desktop"}>
<{script src="coms/pager.js" app="desktop"}>
<{/capture}>
<form method="post" id="fm1" action="index.php?app=finance&ctl=bill&act=importtaobaobill">
  <input type="hidden" name="appropriation_type" id="appropriation_type" value="1" />
  <div class="tableform">
    <div class="division">
      <table border="0" cellspacing="0" cellpadding="0" class="gridlist">
         <tbody>
          <tr>
            <th colspan="2">账单日期（yyyy-mm）：<input type="text" name="text" id="selectPurchase" >(输入后回车即可)
           &nbsp;&nbsp;
              <input type="button" name="cleanPurchase" id='cleanPurchase' value="清空载入数据">
             
            </td>
            <th></th>
            <td>
                
          </tr>
          <tr style="display:none;">
            <th>调出仓库：</th>
            <td>
               <{if $is_super}>
              <{if count($branch)=='1'}>
              <{$branch.0.name}>
              <select id="from_branch_id" name="from_branch_id" style="display:none;">
                <option value="<{$branch.0.branch_id}>"><{$branch.0.name}></option>
              </select>
              <{else}>
                        <select name="from_branch_id" id="from_branch_id">
                            <option value="">--请选择--</option>
                            <{foreach from=$branch item=item}>
                            <option value='<{$item.branch_id}>'  <{if $from_branch_check == $item.branch_id}>selected="selected"<{/if}>><{$item.name}></option>
                            <{/foreach}>
                        </select><span style="color:red">*</span> <{/if}>
              <{else}>
              <{if !$branch_list}> <font color="red">* 当前没有操作仓库的权限，请联系管理员分配。  *</font>
              <script>
                       $$("input","button","select").set('disabled',true);
                    </script>
              <{else}>
              <{if count($branch_list)=='1'}>
              <{$branch_list.0.name}>
              <select id="from_branch_id" name="from_branch_id" style="display:none;">
                <option value="<{$branch_list.0.branch_id}>"><{$branch_list.0.name}></option>
              </select>
              <{else}>
                     <select name="from_branch_id" id="from_branch_id">
                            <option value="">--请选择--</option>
                            <{foreach from=$branch_list item=item}>
                            <option value='<{$item.branch_id}>' <{if $from_branch_check == $item.branch_id}>selected="selected"<{/if}>><{$item.name}></option>
                            <{/foreach}>
                      </select><span style="color:red">*</span> <{/if}>
              <{/if}>
              <{/if}>
             
            </td>
            <th>调入仓库：</th>
            <td>
                <select name="to_branch_id" id="to_branch_id">
                    <option value="">--请选择--</option>
                    <{foreach from=$all_branch item=item}>
                        <option value='<{$item.branch_id}>' <{if $to_branch_check == $item.branch_id}>selected="selected"<{/if}>><{$item.name}></option>
                    <{/foreach}>
                </select>
                <span style="color:red">*</span>
              <{if !$branch_list}> <font color="red">* 当前没有操作仓库的权限，请联系管理员分配。  *</font>
              <script>
                       $$("input","button","select").set('disabled',true);
                    </script>
              <{/if}>
          </tr>
        </tbody>
      </table>
       <span id='pfba' style="display:none;">
      <label>按基础物料编码索引：</label>
      <input type="text" name="bn" />
      </span>
      <table class="gridlist" id="appropriation_table"  style="margin:4px 0;">
        <thead>
          <tr>
            <th style="width:20%;">序号</th>
            <th style="width:60%;">文件名</th>
            <th style="width:20%;">选择</th>
          </tr>
        </thead>
        <tbody id="dataNode">
          <tr>
            <td colspan="9" style="padding:0;"><div class="note" style="margin:0;"> 暂无信息 </div></td>
          </tr>
        </tbody>
      </table>
      <div align="right"><span id="buy_amount" style="display:none;"></div> </div>  
    <div id="cc" class="noprint table-action"> 
    <{button type="button" id="return-btn" label="导入数据"}><span class="red">文件名称有“汇总”请勿选择</span>
    </div>
  </div>
</form>
<script>
(function(){
var visiTips = new Tips({
        onShow:function(tip,el){
            el.addClass('active');
            tip.setStyle('display','block');
        },
        text: function(element){
            if(element.get('visibility')=='false'){
                return '不可售商品';
            }else{
                return '';
            }
        }
    });

    var callurl='index.php?app=console&ctl=admin_appropriation&act=getProducts&p[0]='+$('from_branch_id').value + '&p[1]='+$('to_branch_id').value,store=[];
    var options={
        'getVar':'bn',
        'fxOptions':false,
        callJSON:function(){return window.autocompleter_json;},
        injectChoice:function(json){    
            var choice = new Element('li', {'html': this.markQueryValue(json[this.options.getVar])});
            choice.store('_data',json);
            choice.inputValue = json[this.options.getVar];
            this.addChoiceEvents(choice).inject(this.choices);
        },
        onHide:function(){
            if(!this.selected)return;
            var json=this.selected.retrieve('_data');
            json=$splat(json);
            init(json);
            MessageBox.success('加载成功!!');
        },
        onFocus:function(ipt){
            this.url = 'index.php?app=console&ctl=admin_appropriation&act=getProducts&p[0]='+$('from_branch_id').value + '&p[1]='+$('to_branch_id').value;
            ipt.value='';
        }
    };
   

   
    var tpl='<tr key="{product_id}" id="product_{product_id}" title="点击选取/反选此条记录">'+
        '<td style="display:none"><input type="checkbox" name="stockapp_ID[{product_id}]" key="num"  tname="at[_PRIMARY_]" class="product_select"></td>'+
        '<td><input type="hidden" value="{fileurl}" name="stockapp_ID[{product_id}][fileurl]" key="num"  tname="at[_PRIMARY_]" size="20">{product_id}</td>'+
        '<td>{filename}</td>'+
        '<td><input type="checkbox" name="stockapp_ID[{product_id}][chk]" key="num"  tname="at[_PRIMARY_]"></td>'+
        '<td style="display:none"><{img src="bundle/delecate.gif" app="desktop" key="state" class="pointer btn-delete-item"}></td>'+
        '</tr>';

    var pag;
    function emptyData(){
        var noData='<tr>'+
            '<td colspan="9" style="padding:0;"><div class="note" style="margin:0;"> 暂无信息 </div></td>'+
            '</tr>';
        $('dataNode').set('html',noData);
    }
    function createProduct(data){
        pag=new PageData(tpl,data,{'updateMain':$('dataNode'),'pageNum':10000,
            'format': function(d) {
                d.count = (d.num * d.price).toFixed(3);
            },
            'onShow':function(){
                var _this=this;  
            }
        });
    }


    function init(rs){
        var tmparr=findProduct(rs,'product_id');
         store.unshift.apply(store,tmparr.reverse());
         createProduct(store);
    }
    function findProduct(arr,PRIMARY){
        if(!store.length)return arr;
        store.each(function(a){
            arr.each(function(b){
                if(a[PRIMARY]==b[PRIMARY])
                {
                    
                    a.num=b.num;
                    arr.erase(b);
                }
            });
        });
        return arr;
    }
    function delProduct(obj,arr){
        arr.each(function(d){obj.delData(d);});
    }
    
    
    if($('return-btn'))
    {
      $('return-btn').addEvent('click',function(e)
      {
          doSubmit(this,e);
      });
    }
    
    function doSubmit(el,e){
        var _this=el;
        var form=el.getParent('form');
        if(pag){
            var data=pag.toHideInput($('dataNode').getElement('tr'));
            form.store('target',{extraData:data,
                onRequest:function(){
                    _this.disabled=true;
                },
                onComplete:function(jsontext){
                    try{
                        var json = JSON.decode(jsontext);
                        if (typeof(json.error)!='undefined'){
                            _this.disabled=false;
                        }else{
                            _this.disabled=true;
                            if(opener.finderGroup['<{$env.get.finder_id}>']) opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(100,opener.finderGroup['<{$env.get.finder_id}>']);
                            setTimeout('window.close()',200);
                        }
                    }catch(e){}
                }
            });
        }
        form.fireEvent('submit',e);
    }

    $('selectPurchase').addEvent('enter',function(e){
        var purchase_bn = this.value;
        if(purchase_bn=='')
        {
           return MessageBox.error('<{t}>请选择账期!<{/t}>');   
        }
        
        new Request({url:'index.php?app=finance&ctl=bill&act=gettaobaofile&bill_date='+purchase_bn, method:'get',
          onRequest: function(){
            MessageBox.show('正在加载...');
          },
          onSuccess:function(json){
            if(!json) return;
            rs=JSON.decode(json);
         /*   {"branch_id":"1","po_id":"175","total_nums":99999,"items":[{"item_id":"299","po_id":"175","product_id":"146","num":"99999","in_num":"99999","out_num":"0","price":"0.000","status":"3","barcode":"BR3136S0606","bn":"BR3136S0606","name":"\u749e\u7af9\u62bd\u7eb8S\u78016\u5305\u88c5\uff08\u7bb1\uff09","spec_info":null,"memo":null,"defective_num":"0","from_branch_num":"99999","to_branch_num":0}]}*/
            init(rs);
            
          }
        }).send();
    
    });

    $('cleanPurchase').addEvent('click',function(e){
        $('to_branch_id').options[0].selected=true;
        emptyData();
    });

})();

</script>